package com.woniu.backparking.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.backparking.dto.PageResult;
import com.woniu.backparking.entity.Admin;
import com.woniu.backparking.entity.Complaint;
import com.woniu.backparking.jwtUtil.JwtUtil;
import com.woniu.backparking.service.AdminService;
import com.woniu.backparking.service.ComplaintService;
import com.woniu.backparking.util.BaseEnum;
import com.woniu.backparking.util.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 投诉表 前端控制器
 * </p>
 *
 * @author HashSong
 * @since 2021-07-09
 */
@RestController
@RequestMapping("/complaint")
@CrossOrigin
@Slf4j
public class ComplaintController {
    @Resource
    private ComplaintService complaintService;
    @Resource
    private AdminService adminService;

    /*
    分页条件查询投诉申请：
    参数 分页条件 投诉处理者名字 投诉处理状态
    return 该页投诉内容
     */
    @GetMapping("queryAll")
    @Cacheable(cacheNames = "queryAllCPLT",key="#pageResult.current+#pageResult.name01+#pageResult.state01")
    public JSONResult queryAll(PageResult pageResult){
        log.info("条件查询投诉内容参数:{}-{}",pageResult.getName01(),pageResult.getState01());
        List<Complaint> complaints = complaintService.select01(pageResult);
        return new JSONResult(BaseEnum.StatusCode.Success, BaseEnum.StatusMessage.Success,null,complaints);
    }
    /*
    对投诉内容进行处理消息反馈和处理记录
    参数 投诉反馈内容 投诉id
    return 无
     */
    @PutMapping("complaintBack")
    @CacheEvict(cacheNames = "queryAllCPLT",key="#current+#name01+#state01")
    public JSONResult complaintBack(int current,String name01,int state01,Complaint complaint,@RequestHeader("token")String token) throws Exception {
        log.info("投诉单处理信息返回参数:内容{}-ID{}",complaint.getResult(),complaint.getComplaintId());
        String id= JwtUtil.getId(token);
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_id",id);
        Admin one = adminService.getOne(wrapper);
        complaint.setAdminName(one.getAdminName());
        complaint.setMessageState(1);
        complaintService.updateById(complaint);
        return new JSONResult(BaseEnum.StatusCode.Success, BaseEnum.StatusMessage.Success,null,null);
    }

}

